﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace hearthstone_bepInEx
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        public static List<LettuceTeam> heroTeams;
        private void Form1_Load(object sender, EventArgs e)
        {
            base.SuspendLayout();
            this.runningButton.Text = Main.isRunning ? "结束" : "运行";
            this.findSurpriseCheckBox.Checked = Main.findSurprise;
            List<int> battleCnt = new List<int> { 2, 3, 4, 5 };
            this.battleList.SelectedIndex = battleCnt.FindIndex(t => t == Main.maxBattle);
            heroTeams = CollectionManager.Get().GetTeams();
            if (heroTeams == null || heroTeams.Count == 0)
            {
                SceneMgr.Get().SetNextMode(SceneMgr.Mode.LETTUCE_VILLAGE, SceneMgr.TransitionHandlerType.SCENEMGR, null, null);
                UIStatus.Get().AddInfo("请先进入佣兵模式");
                Form form = (Form)sender;
                form.Close();
            }
            heroTeams.ForEach(i => teamList.Items.Add(i.Name));

            int teamIndex = heroTeams.FindIndex(t => t.ID == Main.teamId);
            if (teamIndex < 0) teamIndex = 0;
            teamList.SelectedIndex = teamIndex;


            List<LettuceBountyDbfRecord> boss = new List<LettuceBountyDbfRecord>();
            for (int i = 1; i < 200; i++)
            {
                LettuceBountyDbfRecord record = GameDbf.LettuceBounty.GetRecord(i);
                if (record != null)
                {
                    boss.Add(record);
                }
            }
            bossList.DataSource = boss;
            bossList.DisplayMember = "ID";
            bossList.ValueMember = "ID";
            bossList.SelectedIndex = boss.FindIndex(t => t.ID == Main.bossId);
            base.ResumeLayout(false);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Main.isRunning = !Main.isRunning;
            Main.isRunningConfig.Value = Main.isRunning;
            this.runningButton.Text = Main.isRunning ? "结束" : "运行";
        }

        private void findSurpriseCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            Main.findSurprise = Main.findSurpriseConfig.Value = findSurpriseCheckBox.Checked;
        }

        private void teamList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Main.teamId = Main.teamIdConfig.Value = heroTeams[teamList.SelectedIndex].ID;
        }

        private void bossList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Main.bossId = Main.bossIdConfig.Value = int.Parse(bossList.SelectedValue.ToString());
        }

        private void battleList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Main.maxBattle = Main.maxBattleConfig.Value = int.Parse(battleList.SelectedItem.ToString());
        }

        private void bossList_Format(object sender, ListControlConvertEventArgs e)
        {
            LettuceBountyDbfRecord record = (LettuceBountyDbfRecord)e.ListItem;
            string heroic = record.Heroic ? "英雄" : "普通";
            e.Value = heroic + " " + record.BountySetRecord.Name.GetString(true) + " " + record.FinalBossCardRecord.Name.GetString(true);
        }

    }
}
